#include "qfontproperty.h"

#include "qintproperty.h"
#include "qboolproperty.h"
#include "qenumproperty.h"

#include "../../libs/sharedLib/qbtneditorlabel.h"
#include "../../libs/sharedLib/qcommonstruct.h"

#include <QFont>
#include <QImage>
#include <QPainter>
#include <QTextOption>
#include <QFontDialog>
#include <QVariant>
#include <QFontDatabase>
#include <QApplication>
static QFontDatabase    fontDatabase;

QFontProperty::QFontProperty(SHAREDLIB::QAbstractProperty *parent):
    SHAREDLIB::QAbstractProperty(parent)
{
    m_family=new QEnumProperty(this);
    m_family->setAttribute("property_name","Family");
    m_family->setAttribute("name","Family");
    QStringList list=fontDatabase.families();
    tagComboItem item;
    ComboItems  items;
    foreach(QString str,list)
    {
        item.m_text=str;
        item.m_value=str;
        items.append(item);
    }
    QVariant v;
    v.setValue<ComboItems>(items);
    m_family->setAttribute("items",v);


    m_pointSize=new QIntProperty(this);
    m_pointSize->setAttribute("property_name","Size");
    m_pointSize->setAttribute("name","Size");

    m_bold=new QBoolProperty(this);
    m_bold->setAttribute("property_name","Bold");
    m_bold->setAttribute("name","Bold");


    m_italic=new QBoolProperty(this);
    m_italic->setAttribute("property_name","Italic");
    m_italic->setAttribute("name","Italic");

    m_underline=new QBoolProperty(this);
    m_underline->setAttribute("property_name","Underline");
    m_underline->setAttribute("name","Underline");

    m_strikeout=new QBoolProperty(this);
    m_strikeout->setAttribute("property_name","Strikeout");
    m_strikeout->setAttribute("name","Strikeout");
}

QWidget* QFontProperty::getEditor()
{
    if(attribute("editabled").toBool())
    {
        SHAREDLIB::QBtnEditorLabel *e=new SHAREDLIB::QBtnEditorLabel(this);
        connect(e,SIGNAL(btnClicked()),this,SLOT(buttonClicked()));
        m_editor=e;
    }
    return QAbstractProperty::getEditor();
}

void QFontProperty::setValue(const QVariant &value)
{
    if(!value.isValid())
    {
        return;
    }
    QFont f=value.value<QFont>();
    m_family->setValue(f.family());
    m_bold->setValue(f.bold());
    m_pointSize->setValue(f.pointSize());
    m_italic->setValue(f.italic());
    m_underline->setValue(f.underline());
    m_strikeout->setValue(f.strikeOut());
    SHAREDLIB::QAbstractProperty::setValue(value);
}

QString QFontProperty::valueText()
{

    QFont f=getValue().value<QFont>();
    return f.family();
}

QIcon QFontProperty::valueIcon()
{
    QFont f=getValue().value<QFont>();
    QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
    img.fill(0);
    QPainter p(&img);
    p.setRenderHint(QPainter::TextAntialiasing, true);
    p.setRenderHint(QPainter::Antialiasing, true);
    f.setPointSize(13);
    p.setFont(f);
    QTextOption t;
    t.setAlignment(Qt::AlignCenter);
    p.drawText(QRect(0, 0, 16, 16), QString(QLatin1Char('A')), t);
    return QPixmap::fromImage(img);
}

void QFontProperty::subPropertyChanged(const QVariant &)
{
    if(m_pointSize->getValue().toInt()<=0)
    {
        return;
    }
    QFont f=getValue().value<QFont>();
    f.setBold(m_bold->getValue().toBool());
    f.setPointSize(m_pointSize->getValue().toInt());
    f.setItalic(m_italic->getValue().toBool());
    f.setUnderline(m_underline->getValue().toBool());
    f.setStrikeOut(m_strikeout->getValue().toBool());
    f.setFamily(m_family->getValue().toString());
    setValue(f);
}

void QFontProperty::buttonClicked()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok,getValue().value<QFont>(),(QWidget*)qApp->property("global_widget").toInt());
    if (ok)
    {
        setSignalValue(QVariant(font));
    }
}
